package com.example.filter;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(
        urlPatterns = {"/error.jsp", "/home.jsp", "/customer.jsp"}
)
public class Loginfilter implements Filter {
    public Loginfilter() {
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        if (request.getSession().getAttribute("user") != null) {
            filterChain.doFilter(request, servletResponse);
        } else {
            request.getRequestDispatcher("/login.jsp").forward(request, servletResponse);
        }

    }
}